package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.Pig;
import com.farm.kaiping.po.PigPO;
import com.farm.kaiping.mapper.PigMapper;
import com.farm.kaiping.service.PigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 猪只信息表(Pig)表服务实现类
 *
 * @author makejava
 * @since 2025-01-22 17:08:49
 */
@Service
@Slf4j
@AllArgsConstructor
public class PigServiceImpl extends ServiceImpl<PigMapper, Pig> implements PigService {
    
	private LambdaQueryWrapper<Pig> generateQueryWrapper(PigPO pigPO){
		LambdaQueryWrapper<Pig> queryWrapper = new LambdaQueryWrapper<>();
		// id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getId()), Pig::getId, pigPO.getId());
		queryWrapper.in(ObjectUtil.isNotEmpty(pigPO.getIdList()), Pig::getId, pigPO.getIdList());
		// 所在厂房
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getBuildingId()), Pig::getBuildingId, pigPO.getBuildingId());
		queryWrapper.in(ObjectUtil.isNotEmpty(pigPO.getBuildingIdList()), Pig::getBuildingId, pigPO.getBuildingIdList());
		// 所在栏位
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getPigpenId()), Pig::getPigpenId, pigPO.getPigpenId());
		queryWrapper.in(ObjectUtil.isNotEmpty(pigPO.getPigpenIdList()), Pig::getPigpenId, pigPO.getPigpenIdList());
		// 猪只编号
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getPigNo()), Pig::getPigNo, pigPO.getPigNo());
		// 体重(千克)
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getWeight()), Pig::getWeight, pigPO.getWeight());
		// 身长(米)
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getLength()), Pig::getLength, pigPO.getLength());
		// 年龄
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getAge()), Pig::getAge, pigPO.getAge());
		// 出生日期
		queryWrapper.like(ObjectUtil.isNotEmpty(pigPO.getBirth()), Pig::getBirth, pigPO.getBirth());
		// 性别
		queryWrapper.like(ObjectUtil.isNotEmpty(pigPO.getGender()), Pig::getGender, pigPO.getGender());
		// 批次
		queryWrapper.like(ObjectUtil.isNotEmpty(pigPO.getBatchNo()), Pig::getBatchNo, pigPO.getBatchNo());
		// 健康状态(健康0/生病1/死亡-1)
		queryWrapper.like(ObjectUtil.isNotEmpty(pigPO.getHealthStatus()), Pig::getHealthStatus, pigPO.getHealthStatus());
		// 销售状态(不可售-1/待售0/已上架1/已售出-2)
		queryWrapper.like(ObjectUtil.isNotEmpty(pigPO.getSalesStatus()), Pig::getSalesStatus, pigPO.getSalesStatus());
		// 删除标记
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getDelFlag()), Pig::getDelFlag, pigPO.getDelFlag());
        queryWrapper.eq(ObjectUtil.isEmpty(pigPO.getDelFlag()), Pig::getDelFlag, 0);
		// 创建人id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getCreateUserId()), Pig::getCreateUserId, pigPO.getCreateUserId());
		// 创建人
		queryWrapper.like(ObjectUtil.isNotEmpty(pigPO.getCreateUserName()), Pig::getCreateUserName, pigPO.getCreateUserName());
		// 创建时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getCreateTime()), Pig::getCreateTime, pigPO.getCreateTime());
		// 更新时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getUpdateTime()), Pig::getUpdateTime, pigPO.getUpdateTime());
		// 更新人
		queryWrapper.like(ObjectUtil.isNotEmpty(pigPO.getUpdateUserName()), Pig::getUpdateUserName, pigPO.getUpdateUserName());
		// 更新人id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getUpdateUserId()), Pig::getUpdateUserId, pigPO.getUpdateUserId());
		// 租户id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigPO.getTenantId()), Pig::getTenantId, pigPO.getTenantId());
		// 创建时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(pigPO.getCreateTimeBetween(),0)), Pig::getCreateTime,StringRange.getOne(pigPO.getCreateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(pigPO.getCreateTimeBetween(),1)), Pig::getCreateTime,StringRange.getOne(pigPO.getCreateTimeBetween(),1));
		// 更新时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(pigPO.getUpdateTimeBetween(),0)), Pig::getUpdateTime,StringRange.getOne(pigPO.getUpdateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(pigPO.getUpdateTimeBetween(),1)), Pig::getUpdateTime,StringRange.getOne(pigPO.getUpdateTimeBetween(),1));

		queryWrapper.orderByDesc(Pig::getCreateTime,Pig::getId);
		return queryWrapper;
	}

	 /**
	  * @describe 分页查询猪只信息表
	  * @author makejava
	  * @date 2025-01-22 17:08:49
	  */
	@Override
	public Page<Pig> getPigPage(Page page, PigPO pigPO){
		LambdaQueryWrapper<Pig> queryWrapper = this.generateQueryWrapper(pigPO);
        return this.baseMapper.selectPage(page, queryWrapper);
	}
	
	/**
     * @describe 查询猪只信息表list
     * @author  makejava
     * @date  2025-01-22 17:08:49
     */
	@Override
	public List<Pig> getPigList(PigPO pigPO){
		LambdaQueryWrapper<Pig> queryWrapper = this.generateQueryWrapper(pigPO);
        return this.baseMapper.selectList(queryWrapper);
	}
	
	/**
     * @describe 获取一条数据 猪只信息表
     * @author  makejava
     * @date  2025-01-22 17:08:49
     */
	@Override
	public Pig getPig(PigPO pigPO){
		LambdaQueryWrapper<Pig> queryWrapper = this.generateQueryWrapper(pigPO);
        queryWrapper.last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
	}

    /**
     * @describe 新增猪只信息表
     * @author  makejava
     * @date  2025-01-22 17:08:49
     */
	@Override
	public Integer savePig(PigPO pigPO) {
		if(ObjectUtil.isEmpty(pigPO.getBuildingId())){
			throw new RuntimeException("所在厂房为空");
		}
		if(ObjectUtil.isEmpty(pigPO.getPigpenId())){
			throw new RuntimeException("所在栏位为空");
		}
		pigPO.setPigNo(new SnowflakeGenerator().next());
        pigPO.setDelFlag(0);
		pigPO.setTenantId(SecurityUtils.getUser().getTenantId());// 租户ID
		pigPO.setCreateTime(LocalDateTime.now());
		pigPO.setCreateUserId(SecurityUtils.getUser().getUserId());// 创建人
		pigPO.setCreateUserName(SecurityUtils.getUser().getRealName());// 创建人
		pigPO.setUpdateTime(LocalDateTime.now());
		Pig pig = BeanUtil.copyProperties(pigPO, Pig.class);
		this.save(pig);
		return pig.getId();
	}

	/**
     * @describe 修改猪只信息表
     * @author  makejava
     * @date  2025-01-22 17:08:49
     */
	@Override
	public String updatePig(PigPO pigPO) {
		if (ObjectUtil.isEmpty(pigPO.getId())) {
			throw new RuntimeException("id为空");
		}

		pigPO.setUpdateUserId(SecurityUtils.getUser().getUserId());// 更新人
		pigPO.setUpdateUserName(SecurityUtils.getUser().getRealName());// 更新人
		pigPO.setUpdateTime(LocalDateTime.now());
		Pig pig = BeanUtil.copyProperties(pigPO, Pig.class);
		this.updateById(pig);
		return "修改成功";
	}

	/**
     * @describe 批量修改猪只信息表
     * @author  makejava
     * @date  2025-01-22 17:08:49
     */
	@Override
	public String batchUpdate(PigPO pigPO) {
		if (ObjectUtil.isEmpty(pigPO.getBatchNo())) {
			throw new RuntimeException("批次为空");
		}

		this.update(new LambdaUpdateWrapper<Pig>().eq(Pig::getBatchNo,pigPO.getBatchNo())
				.set(Pig::getUpdateUserId,SecurityUtils.getUser().getUserId())
				.set(Pig::getUpdateUserName,SecurityUtils.getUser().getRealName())
				.set(Pig::getUpdateTime,LocalDateTime.now())
				// 所在厂房
				.set(ObjectUtil.isNotEmpty(pigPO.getBuildingId()), Pig::getBuildingId, pigPO.getBuildingId())
				// 所在栏位
				.set(ObjectUtil.isNotEmpty(pigPO.getPigpenId()), Pig::getPigpenId, pigPO.getPigpenId())
				// 体重(千克)
				.set(ObjectUtil.isNotEmpty(pigPO.getWeight()), Pig::getWeight, pigPO.getWeight())
				// 身长(米)
				.set(ObjectUtil.isNotEmpty(pigPO.getLength()), Pig::getLength, pigPO.getLength())
				// 年龄
				.set(ObjectUtil.isNotEmpty(pigPO.getAge()), Pig::getAge, pigPO.getAge())
				// 出生日期
				.set(ObjectUtil.isNotEmpty(pigPO.getBirth()), Pig::getBirth, pigPO.getBirth())
				// 性别
				.set(ObjectUtil.isNotEmpty(pigPO.getGender()), Pig::getGender, pigPO.getGender())
				// 健康状态(健康0/生病1/死亡-1)
				.set(ObjectUtil.isNotEmpty(pigPO.getHealthStatus()), Pig::getHealthStatus, pigPO.getHealthStatus())
				// 销售状态(不可售-1/待售0/已上架1/已售出-2)
				.set(ObjectUtil.isNotEmpty(pigPO.getSalesStatus()), Pig::getSalesStatus, pigPO.getSalesStatus())
		);
		return "修改成功";
	}

	/**
     * @describe 通过id删除猪只信息表
     * @author  makejava
     * @date  2025-01-22 17:08:49
     */
	@Override
	public String removePig(PigPO pigPO) {
		if (ObjectUtil.isEmpty(pigPO.getBatchDel())) {
			throw new RuntimeException("删除数据未选择");
		}
		LambdaUpdateWrapper<Pig> removeWrapper = new LambdaUpdateWrapper<>();
		removeWrapper.in(Pig::getId, pigPO.getBatchDel());
		removeWrapper.set(Pig::getDelFlag,1);
		removeWrapper.set(Pig::getUpdateUserId,SecurityUtils.getUser().getUserId());// 更新人
		removeWrapper.set(Pig::getUpdateUserName,SecurityUtils.getUser().getRealName());// 更新人
		removeWrapper.set(Pig::getUpdateTime,LocalDateTime.now());

		this.update(removeWrapper);
		return "删除成功";
	}
	
}

